package im.push.impush.wspush.config;

import im.push.impush.wspush.IMReceiver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @author ccl
 * @time 2018-11-01 11:10
 * @name RedisConfig
 * @desc: redis 消息订阅配置
 */
@Configuration
public class RedisConfig {
    /**
     * 消息接收容器配置
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("oim-push"));
        return container;
    }

    /**
     * 接收消息处理器配置
     */
    @Bean
    MessageListenerAdapter listenerAdapter(IMReceiver receiver){
        return new MessageListenerAdapter(receiver, "recvMessage");
    }

    /**
     * 消息发送配置
     */
    @Bean
    StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory){
        return new StringRedisTemplate(connectionFactory);
    }
}
